<?php
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/control/BaseService.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/db/publish/PublishClickedAdDAO.php';
include_once $_SERVER ['DOCUMENT_ROOT'] . '/ad/sys/com/model/util/Util.php';  

class ClickedAdService extends BaseService {
	
	public function __construct(){
		parent::__construct();
	 	parent::setTarget($this);	
	 	$this->log = Plog::factory(__FILE__);	
	}
	


	public function queryReport(){
//		$dateEnd=isset($_GET["dateEnd"])?$_GET["dateEnd"]:@date("Y-m-d");
		$dateEnd=@$_GET["dateEnd"];
		$account=@$_GET["chartsAccount"];
		$publishClickedAdDAO=new PublishClickedAdDAO();
		$result=$publishClickedAdDAO->queryReport($account, $dateEnd);
		echo "{row:".Util::toJson($result)."}";
		
		
	}

	public function getStatisticsCountDate(){
		$date=@$_GET["date"];
		$id=@$_GET["id"];
		$status=@$_GET["status"];
//		echo "date=$date";
//		echo "id=$id";
//		echo "status=$status";
		$publishClickedAdDAO=new PublishClickedAdDAO();
		$result=$publishClickedAdDAO->getStatisticsCountDate($id, $date, $status);
		echo "{row:".Util::toJson($result)."}";
	}
	
	public function getStatisticsRatioDate(){
		$date=@$_GET["date"];
		$id=@$_GET["id"];
		$status=@$_GET["status"];
		$publishClickedAdDAO=new PublishClickedAdDAO();
		$result=$publishClickedAdDAO->getStatisticsRatioDate($id, $date, $status);
		echo "{row:".Util::toJson($result)."}";
	}
	public function exportExcel(){
		
		$date=@$_GET["date"];
		$id=@$_GET["id"];
		$status=@$_GET["status"];
		$type=@$_GET["type"];
		$result=null;
		$publishClickedAdDAO=new PublishClickedAdDAO();
		$title=null;
		switch($type){
			case 0:
				$result=$publishClickedAdDAO->getStatisticsCountDate($id, $date, $status);
				$title="曝光數";//"點擊數";"點擊率";"曝光率";
				break;
			case 1:
				$result=$publishClickedAdDAO->getStatisticsCountDate($id, $date, $status);
				$title="點擊數";//"點擊率";"曝光率";
				break;
			case 2:
				$result=$publishClickedAdDAO->getStatisticsRatioDate($id, $date, $status);
				$title="點擊率";//"曝光率";
				break;
			case 3:
				$result=$publishClickedAdDAO->getStatisticsRatioDate($id, $date, $status);
				$title="曝光率";
				break;
		}
		$filename = "Ad Report";
		$en = array("A","B");
		$titles =array(
			"$en[0]1" => $title,
		    "$en[1]1" => "日期"
		);
		
		$contents = array();
		$j=2;
		foreach ($result as $obj) {
			$contents[] = array(
				"$en[0]$j" => $obj["Sum"],
			    "$en[1]$j" => $obj["Date"]
			);
			$j++; 
		}
		//匯出Excel
		echo "type=$type <br/>";
		echo "date=$date <br/>";
		echo "status=$status <br/>";
		echo "id=$id <br/>";
		echo "{row:".Util::toJson($result)."}";
//		Util::excel($filename, $titles, $contents); 
	}
}
$service =  new ClickedAdService();
$service->__call($_GET["action"], null);
//$service->queryTargetAccountMember();
?>